import { Handler } from "./Handler";
import { any } from "../../../core/logic";
import { Channel, Sampler,Animation} from "../../../component/animation/index";
class ZoomMouse extends Handler{
    constructor(opts={}){
        super(opts);
        this.ratio=any(opts.ratio,1.5);
        this.easing=any(opts.easing,"easeOut_Cubic");//tween.easeOut.Expo
        this.dual=any(opts.dual,0.5);//单位：s
        this.zoomMode=any(opts.zoomMode,"points");//points|center
        this.maxDistance=any(opts.maxDistance,Number.MAX_VALUE);
        this.minDistance=any(opts.minDistance,0);
        this.pre=null;//上一组值
        this.cur=null;//当前值
        this.register();
    }
    set zoomMode(v){
        this._zoomMode=v;
        if(this.zoomMode==="points"){
            this.zoom=this.zoomPoints;
        }else{
            this.zoom=this.zoomCenter;
        }
        return this;
    }
    get zoomMode(){
        return this._zoomMode;
    }
    register(){
        this.on("wheel",this.update);
    }
    update(event){
        if(!this.avalid){
            return;
        }
        this.animation.stop();
        this.animation.clear();
        this.zoom(event);
    }
    zoomPoints(event){
        //计算鼠标点位置
        let scale=Math.pow(this.ratio,-event.data.zoom);
        let distance=this.target.trans.position.distanceTo(this.target.at);
        if(distance*scale>=this.maxDistance||distance*scale<=this.minDistance){
            return;
        }
        let points=this.target.pixelToWorld(event.data.pixel);
        //计算目标点位置
        let direction=this.target.at.clone();
        direction.sub(points);
        let at=points.clone();
        at.addScaledVector(direction,scale);
         //计算相机位置
        let v=this.target.trans.position.clone().sub(this.target.at);
        let position=at.clone();
        position.addScaledVector(v,scale);
        if(this.dual<=0){
            this.target.trans.position=position;
            this.target.at=at.toArray();
        }else{
            let sampler=new Sampler([0,this.dual],[this.target.at.toArray(),at.toArray()],"LINEAR",this.easing);
            let channel=new Channel(this.target,"at");
            channel.add(sampler);
            let sampler1=new Sampler([0,this.dual],[this.target.trans.position.toArray(),position.toArray()],"LINEAR",this.easing);
            let channel1=new Channel(this.target.trans,"position");
            channel1.add(sampler1);
            this.animation.add(channel).add(channel1);
        }
        this.animation.start();
    }
    zoomCenter(event){
        let scale=Math.pow(this.ratio,-event.data.zoom);
        let distance=this.target.trans.position.distanceTo(this.target.at);
        if(distance*scale>=this.maxDistance||distance*scale<=this.minDistance){
            return;
        }
        //计算相机位置
        let v=this.target.trans.position.clone().sub(this.target.at);
        let position=this.target.at.clone();
        position.addScaledVector(v,scale);
        if(this.dual<=0){
            this.target.trans.position=position.toArray();
        }else{
            let sampler=new Sampler([0,this.dual],[this.target.trans.position.toArray(),position.toArray()],"LINEAR",this.easing);
            let channel=new Channel(this.target.trans,"position");
            channel.add(sampler);
            this.animation.add(channel);
        }
        this.animation.start();
    }
}
export {ZoomMouse}